﻿using JVMTI_CONN_CONNECT;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;

namespace JVMTI_CORE_SHELL
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        public static SolidColorBrush AccentBrush
        {
            get
            {
                return App.Current.Resources["AccentBrush"] as SolidColorBrush;
            }
        }
        //ugly, but needs to broadcast to all grids.
        public static double MaximumThreadEndTime;// = threads.Max<ThreadLifeCycleViewModel>(o => o.EndTimeStamp - o.StartTimeStamp);
        public static double MinimumStartTime;// = threads.Min<ThreadLifeCycleViewModel>(o => o.StartTimeStamp);
        public static double MaximumDrawingWidth { get { return (App.Current.MainWindow as MainWindow).lifeCycleView.ActualWidth - 200; } }
        public static CodeViewer CodeViewerWindow;
        public static IEnumerable<ILifeEvent> RawEvents;
        public static void RelaunchWithSelection(Window requester)
        {
            OpenFileDialog diag = new OpenFileDialog();
            diag.AddExtension = true;
            diag.Filter = "ORCA files (*.orca)|";
            if (diag.ShowDialog() == DialogResult.OK && diag.FileName != "")
            {
                UserPreference.Default.PreviousFile = diag.FileName;
                UserPreference.Default.Save();
                Startup startUp = new Startup();
                startUp.Show();
                System.Windows.Application.Current.MainWindow = startUp;
                requester.Close();
            }
        }
        public static string FileNameInUse;
        public static string searchForFiles(string fileName)
        {
            var paths = UserPreference.Default.SearchPath.Split(';');
            if (File.Exists(fileName)) return fileName;
            var p = paths.FirstOrDefault<string>(path => File.Exists(System.IO.Path.Combine(path,fileName)));
            if (p == null) return null;
            else return System.IO.Path.Combine(p, fileName);
        }

    }
}
